'''
中文名
别名
原产地
功能
信息介绍
历史
适合人群

友善
颜值
粘人程度
聪明度
忠诚度

'''


import requests
from bs4 import BeautifulSoup
import sqlite3
url = 'https://www.chongso.com/z/chongwuffg/'
rq = requests.get(url)

rq.encoding = rq.apparent_encoding;
#print(rq)
# soup = BeautifulSoup(rq.text,'lxml')
soup = BeautifulSoup(rq.text,'html.parser')
#print(soup.h3.get_text())
#print(soup)

tag1 = soup.find_all('div',class_='dq_dbox')
tag2 = soup.find_all('div',class_='dq_dtwo clearfix')
tag3 = soup.find_all('div',class_='tab-pal')
text = []
msg = [] #宠物自我介绍存放
his = [] #历史来源
fit = [] #适合人群
#print(tag1)

for line1 in tag1:
    line = line1.find_all('div',class_='plc')
    #print(line)
    for i in line:
        if i.string is not None:
            #print(i.string)
            text.append(i.string)

for lines in tag1:
    line = lines.find_all('span')
    for i in line:
        if i.string is not None:
            text.append(i.string)
#宠物自我介绍
#print(tag3)
# for line in tag3:
#     res1=line.find_all('p')[0]
#     recieve = (res1.string).strip('\r\n\t')
#     msg.append(recieve)

# for line in tag3:
#     res1=line.find_all('p')[1]
#     his.append(res1.string)
#
# for line in tag3:
#     res1=line.find_all('p')[2]
#     fit.append(res1.string)
try:
    for line in tag3:
        res1=line.find_all('p')[0]
        recieve = (res1.string).strip('\r\n\t')
        msg.append(recieve)
    for line in tag3:
        res1=line.find_all('p')[1]
        his.append((res1.string).strip('\r\n\t'))
    for line in tag3:
        res1=line.find_all('p')[2]
        fit.append((res1.string).strip('\r\n\t'))

except:
    msg.append('没有找到信息')
    his.append('没有找到信息')
    fit.append('没有找到信息')
    print('没有找到')




# print(msg)
# print(his)

for line2 in tag2:
    line = line2.find_all('em')
    #print(line)
    for i in line:
        indexnum=str(i).index(':') #16
        #print(indexnum)
        res=str(i)[indexnum+1:indexnum+4]
        #print(res)
        if res.endswith('%'):

            text.append(res.strip('%'))
            #print(res)
        else:
            text.append(res)




print(text.index('100'))
#print(msg)
# print(his)
# print(fit)

pname=text[0]  #宠物名
oname =text[1] #宠物别名
address =text[2] #宠物来源地
ability=text[28] #宠物功能
pmsg =msg[0] #自我介绍
pet_his=his[0] #宠物历史
pet_fit=fit[0] #适合人群
pet_clingy =text[30] #粘人程度
pet_kind=text[35] #友善
pet_smart = text[44]#聪明
pet_honest=text[45]#忠诚
bueaty=text[46] #颜值





# (pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty)
#数据库处理部分
# conn = sqlite3.connect('test.sqlite')
# def insert_data(pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty):
#
#   sql = "insert into pet_msg (pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty) values\
#   ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')"%(pname,oname,address,ability,pmsg,pet_his,pet_fit,pet_clingy,pet_kind,pet_smart,pet_honest,bueaty)
#   try:
#     cursor = conn.cursor()
#     cursor.execute(sql)
#     return cursor.fetchall()
#   finally:
#     conn.commit()
#     conn.close()
# insert_data(pname=pname,oname=oname,address=address,ability=ability,pmsg=pmsg,pet_his=pet_his,pet_fit=pet_fit,\
# pet_clingy=pet_clingy,pet_kind=pet_kind,pet_smart=pet_smart,pet_honest=pet_honest,bueaty=bueaty)


